<template>
  <div class="body">
    <el-form ref="ruleForm" :model="mdl" label-width="130px">
      
        <el-form-item label="活动图片" required>
          <el-upload
            ref="upload"
            list-type="picture-card"
            action
            :file-list="fileList"
            :show-file-list="true"
            :http-request="uploadFile"
            accept="image/gif,image/jpeg,image/jpg,image/png,image/svg"
            :limit="1"
            :on-change="handlePreview"
            :on-exceed="handleExceed"
            :on-remove="handeRemove"
          >
            <i class="el-icon-plus" />
          </el-upload>
        </el-form-item>
        <el-form-item label="活动名称" required>
          <el-input
            v-model="mdl.name"
            placeholder="请输入活动名称"
          />
        </el-form-item>
        <el-form-item label="跳转地址" required>
          <el-input
            v-model="mdl.jumpAddress"
            placeholder="比如：/pages/index/main"
          />
        </el-form-item>
        <el-form-item label="上下架" required>
          <el-switch
            v-model="status"
            active-color="#13ce66"
            inactive-color="#ff4949">
          </el-switch>
        </el-form-item>
        
      
    </el-form>
    <div class="foot_btn">
      <el-button
        type="primary"
        :loading="submitLoading"
        @click="submitForm('ruleForm')"
      >提交</el-button>
      <el-button @click="handleCloseDrawer">取消</el-button>
    </div>
  </div>
</template>

<script>
import { fileUpload,addActivity,updateActivity } from '@/api/user'
export default {
  name: 'FormModel',
  props: {
    mdl: {
      type: Object,
      required: true
    },
    handleCloseDrawer: {
      type: Function,
      required: true
    }
  },
  data() {
    return {
      submitLoading: false,
      fileList: [],
      uploadData: null,
      img: '',
      status:false
    }
  },
  mounted() {
    if(this.mdl.img){
      this.img = this.mdl.img
      this.fileList.push({
        url: this.mdl.img
      })
    }
    if(this.mdl.status){
      this.status = true
    }
  },
  methods: {
    // 移除上传图片
    handeRemove(e){
      this.imgList = ''
    },

    // 上传图片
    uploadFile(e) {
      fileUpload(this.uploadData).then((res) => {
        if (res) {
          // this.mdl.roomImg = res.data
          this.img = res.data
          this.$message({
            message: '上传成功',
            type: 'success'
          })
          console.log(res)
        } else {
          this.$message.error(res.status)
        }
      })
    },
    handleExceed(file) {
      this.$message({
        message: '活动图片只能上传一张',
        type: 'warning'
      })
    },
    // 获取上传文件信息
    handlePreview(file) {
      const _this = this
      var formData = new FormData()
      formData.append('file', file.raw)
      _this.uploadData = formData
    },

    // 提交
    submitForm() {
      if(this.status){// 上架
        this.mdl.status = 1
      }else{// 下架
        this.mdl.status = 0
      }
      this.mdl.img = this.img
      console.log(this.mdl)
      const request = this.mdl.id ? updateActivity(this.mdl) : addActivity(this.mdl)
      request.then((res) => {
        if (res.statusCode === '00000') {
          this.$message({ message: '提交成功', type: 'success' })
          // 关闭弹窗
          this.handleCloseDrawer()
          // 调用父级组件刷新数据
          this.$emit('getList')
        }
      })
        .catch((error) => {
          console.log(error)
        })
        .finally(() => {
        })
    }
  }
}
</script>

<style lang="less" scoped>
.body {
  padding-right: 25px;
  .foot_btn{
    width:150px;
    margin: 0 auto;
  }
}

/deep/ .el-checkbox__label {
  font-size: 12px;
}
</style>
